<?php
//设定字符集
header("Content-type: text/html; charset=utf-8"); 
/**
* php面向对象练习
*/
class ShopProduct
{
    public $title = '商品名称';     
    private $cost = 100;
    public $name = '宝贝';
    public $price = 120;
    protected $sell = 1;

    //定义构造方法，进行对象的初始化工作
    public function __construct($name = '',$price = 0,$cost = 0,$sell = 0)
    {
        $this -> name = $name;
        $this -> price = $price;
        $this -> cost = $cost;
        $this -> sell = $sell;
    }

    //定义类的方法
    public function display()
    {
        return $this->name . '的价格是:' . $this->price;
    }
}

/**
* 定义书籍类
*/
class Book extends ShopProduct
{
    public $author = '';
    public $version = 1;
    public $press = '';

    function __construct($name = '',$price = 0,$cost = 0,$sell = 0,$author = '',$version = 1,$press = '')
    {
        parent::__construct($name,$price,$cost,$sell);
        $this -> author = $author;
        $this -> version = $version;
        $this -> press = $press;
    }
}

//对象的生成
$p1 = new ShopProduct();
$p2 = new ShopProduct('小米6',2999,2000,2000);

$p3 = new Book('PHP核心语法',29.99,2.9,100,'John',1,'机械工业出版社');

echo '<pre>';
echo $p3->display();
echo '</pre>';

//改变对象的属性
// $p1->title = '书籍';
// echo $p1->title;

//以下内容会报错，private和protected属性不能在对象中访问
// echo $p1->sell;
?>